package com.aly.game.exe2lua.builder;

import com.aly.game.exe2lua.enums.ExportConfig;
import com.aly.game.exe2lua.funcs.TypeConsumer;
import com.aly.game.exe2lua.objs.*;
import com.aly.game.exe2lua.tool.Pair;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.HashMap;
import java.util.Map;

/**
 * aly @ 16-11-12.
 */
public abstract class BaseExportBuilder {
    private ExportConfig type;

    public BaseExportBuilder(ExportConfig type) {
        this.type = type;
    }

    public final void buildString(StringBuilder lineSb, String cellVal) {
        if (cellVal == null) {
            cellVal = "";
        } else {
            cellVal = cellVal.trim();
        }
        if (cellVal.length() == 0) {
            lineSb.append("\"\"");
        } else {
            boolean b = cellVal.charAt(0) == '\"';
            boolean e = cellVal.charAt(cellVal.length() - 1) == '\"';

            cellVal = cellVal.substring(b ? 1 : 0, e ? cellVal.length() - 1 : cellVal.length());

            lineSb.append("\"").append(cellVal.replaceAll("\"", "\\\\\"")).append("\"");
        }
    }

    /**
     * @param info 头信息 可能不存在
     */
    public final void buildHeadLine(StringBuilder builder, String saveFileName, TypedHeadInfo info) {
        String header = type.getFileHeader();
        if (header != null && header.length() != 0) {
            header = header.replaceAll("\\{dataName}", saveFileName);
        }
        builder.append(header).append("\n");
        buildHeadLine0(builder, saveFileName, info);
    }

    public abstract void buildHeadLine0(StringBuilder builder, String saveFileName, TypedHeadInfo info);

    /**
     * @param id null 没有表头
     */
    public abstract void buildLine(StringBuilder builder, TypedHeadInfo headInfo, String id, LineObj line, boolean buildOne, Map<HeadItem, String> replaceVal);

    public abstract String getSaveFileName(Sheet sheet, String fileName);

    public abstract void buildEnd(StringBuilder builder, TypedHeadInfo headInfo);

    public final Pair<String, Map<HeadItem, String>> getValue(TypedHeadInfo headInfo, LineObj line) {
        HeadItem luaIdxHead = getIDHeadItem(headInfo);
        String ID = null;
        if (null != luaIdxHead) {
            ID = line.getValue(luaIdxHead);
        }
        Map<HeadItem, String> lineMap = new HashMap<>();
        for (HeadItem item : headInfo) {
            String cellVal = line.getValue(item);
            if (cellVal.length() == 0) continue;
            if (luaIdxHead == item) continue;
            lineMap.put(item, cellVal);
        }
        return Pair.valueOf(ID, lineMap);
    }

    public abstract HeadItem getIDHeadItem(TypedHeadInfo headInfo);

    public final String getID(LineObj lineObj, TypedHeadInfo headInfo) {
        HeadItem idHeadItem = getIDHeadItem(headInfo);
        if (null == idHeadItem) return null;
        return lineObj.getValue(idHeadItem);
    }

    public final void parseHead(String cellVal, TypeConsumer consumer) {
        String typeAlia;
        if (cellVal == null || "".equals(cellVal)) {
            typeAlia = "";
        } else {
            final int ind = cellVal.indexOf(type.getHeadFix()[0]);
            final int ind2 = cellVal.indexOf(type.getHeadFix()[1]);
            if (ind == -1 || ind2 == -1) {
                typeAlia = "";
            } else {
                typeAlia = cellVal.substring(ind + 1, ind2).trim();
            }
        }
        parseHead(typeAlia, cellVal, consumer);
    }


    protected abstract void parseHead(String typeAlia, String cellVal, TypeConsumer consumer);
}
